home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / nt16322.zip / netedit.c_ / netedit.c
C/C++ Source or Header  |  1996-07-06  |  3KB  |  114 lines

  1.  /**********************************************************************
  2.  *                   InterSoft International, Inc                      *
  3.  *                        Copyright (C) 1995                           *
  4.  ***********************************************************************
  5.  * System:   IBM PC                                                    *
  6.  * Program:  NETEDIT.C                                                 *
  7.  * Author:   K.R. Robinette                                            *
  8.  * Date:     July, 1996                                                *
  9.  * Function: Remote Editing Support                                    *
  10.  **********************************************************************/
  11. #include "stdio.h"
  12. #include "string.h"
  13. #include "sys/types.h"
  14. #include "sys/stat.h"
  15.  
  16.  char on[5]  = {"\033[5i"};
  17.  char off[5] = {"\033[3i"};
  18.  
  19.  main(argc,argv)
  20.  int argc;
  21.  char **argv;
  22.  {
  23.  int  len,flag,mode;
  24.  FILE *fd;
  25.  char line[1024],out[1024];
  26.  struct stat buf;
  27.  if(argc == 2)
  28.       {
  29.       if((stat(argv[1],&buf)) != 0)
  30.            {
  31.            printf("Error, could not open %s\n",argv[1]);
  32.            exit(-1);
  33.            }
  34.       mode = buf.st_mode;
  35.       if((fd = fopen(argv[1],"r")) == NULL)
  36.            {
  37.            printf("Error, could not open %s\n",argv[1]);
  38.            exit(-1);
  39.            }
  40.       if((fwrite(on,1,4,stdout)) != 4)
  41.            {
  42.            printf("Error, writing to network\n");
  43.            exit(-1);
  44.            }
  45.       while(1)
  46.            {
  47.            if(fgets(line,1023,fd) == NULL)
  48.                 break;
  49.            len = strlen(line);
  50.            fwrite(line,1,len,stdout);
  51.            }
  52.       fwrite(off,1,4,stdout);
  53.       }
  54.       else
  55.            {
  56.            printf("Input filename required\n");
  57.            exit(-2);
  58.            }
  59.  fclose(fd);
  60.  
  61.  fd = NULL;
  62.  system("stty -echo");
  63.  strcpy(out,argv[1]);
  64.  strcat(out,".new");
  65.  line[0] = 0;
  66.  while(1)
  67.       {
  68.       if(fgets(line,sizeof(line)-1,stdin) == NULL)
  69.            {
  70.            flag = 2;
  71.            break;
  72.            }
  73.       if(line[0] == 0x02)
  74.            {
  75.            system("stty echo");
  76.            printf("File was not modified\n");
  77.            exit(0);
  78.            }
  79.       if(fd == NULL)
  80.            if((fd = fopen(out,"w")) == NULL)
  81.                 {
  82.                 system("stty echo");
  83.                 printf("Error, could not open output file %s\n",out);
  84.                 exit(-3);
  85.                 }
  86.       if(line[0] == 0x01)
  87.            {
  88.            flag = 1;
  89.            break;
  90.            }
  91.       len = strlen(line);
  92.       fwrite(line,1,len,fd);
  93.       }
  94.  if(fd)
  95.       fclose(fd);
  96.  if(flag == 2)
  97.       remove(out);
  98.       else if(flag == 1)
  99.            {
  100.            remove(argv[1]);
  101.            strcpy(line,"mv ");
  102.            strcat(line,out);
  103.            strcat(line," ");
  104.            strcat(line,argv[1]);
  105.            system(line);
  106.            chmod(argv[1],mode);
  107.            }
  108.  system("stty echo");
  109.  if(flag == 1)
  110.       printf("File was modified\n");
  111.       else
  112.       printf("File was not modified\n");
  113.  }
  114.